package 一些算法竞赛测试题;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-10-04
 * Time: 14:27
 */

/*
广度优先搜索(BFS)的迷宫问题
 */

class pair{
    public int x;
    public int y;

    public pair(int x, int y){
        this.x = x;
        this.y = y;
    }
}

public class Demo9 {
    private static int[] getX = {0, 1, 0, -1};
    private static int[] getY = {1, 0, -1, 0};

    public static boolean BFS(int[][] Mat, int startx, int starty, int endx, int endy){
        //用队列保存搜索到的位置
        Queue<pair> posQ = new LinkedList<>();
        posQ.offer(new pair(startx, starty));
        int row = Mat.length;
        int col = Mat[0].length;

        int[][] book = new int[row][col];
        book[startx][starty] = 1;

        //搜索
        while(!posQ.isEmpty()){
            pair curPos = posQ.poll();
            if(curPos.x == endx && curPos.y == endy){
                return true;
            }
            //搜索新的位置
            for(int i = 0; i < 4; i++){
                int newx = curPos.x + getX[i];
                int newy = curPos.y + getY[i];
                if(newx >= 0 && newx < row && newy >= 0 && newy < col && Mat[newx][newy] == 0 && book[newx][newy] == 0){
                    posQ.offer(new pair(newx, newy));
                    book[newx][newy] = 1;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[][] Mat = {{0, 0, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 0}, {1, 1, 0, 0}};
        Scanner scanner = new Scanner(System.in);
        int startx = scanner.nextInt();
        int starty = scanner.nextInt();
        int endx = scanner.nextInt();
        int endy = scanner.nextInt();
        System.out.println(BFS(Mat, startx, starty, endx, endy));
    }
}
